// Add friend
function addFriend()
{
	var friend = $('#nameFriend').val();
	var user = $('#select').text();
	var requestURL = "http://localhost:53220/api/friendlist?user=" + user + "&fr=" + friend;
	$.ajax({
		url: requestURL,
		type: 'POST',
		success: function(data){
			alert("Add friend, wait your friend accept!");
			$('#friendDialog').dialog('close');
		}
	});
}

// Accept friend
function acceptFriend(friend)
{
	var user = $('#select').text();
	var requestURL = "http://localhost:53220/api/acceptfriend?user=" + user + "&friend=" + friend + "&type=1";
	$.ajax({
		url: requestURL,
		type: 'POST',
		success: function(data){
			if(data != null)
				alert("Add friend success!");
			else
				alert("Error");
		},
		error: function(){ alert('Error');}
	});
}

//Delete friend
function deleteFriend(friend)
{
	var user = $('#select').text();
	var requestURL = "http://localhost:53220/api/acceptfriend?user=" + user + "&friend=" + friend + "&type=0";
	$.ajax({
		url: requestURL,
		type: 'POST',
		success: function(data){
			if(data != null)
				alert("Delete friend success!");
			else
				alert("Error");
		},
		error: function(){ alert('Error');}
	});
}

// Get request friend
function requestFriend()
{
	var user = $('#select').text();
	var requestURL = "http://localhost:53220/api/acceptfriend?user=" + user;
	var HTML = "<div id ='accept#{friend}' style='display: inline-block; width:280px'>" + 
    				"<span style='position: relative; top: 5px'>#{friend}</span>" +
         			"<div style='float: right; bottom: 5px'>" +
						"<button class='okFr' value='#{friend}'>Accept</button>" + 
						"<button class='cancelFr' value='#{friend}'>Decline</button>" + 
        			"</div>"  +
    			"</div>"
	$.ajax({
	    url: requestURL,
	    type: 'GET',
	    success: function (data) {
	        if (data != null) {
	            data.forEach(function (fr) {
	                var f = $(HTML.replace(/#\{friend\}/g, fr));
	                $('#acceptFriendDialog').append(f);
	            });
	            $('#acceptFriendDialog').dialog('open');
	        }
	    }
	})	
}
